<?php
namespace app\admin\controller;
use app\admin\BaseController;
use app\admin\model\Category;
use think\facade\Config;
use think\facade\Request;

use think\facade\View;
use think\model\concern\SoftDelete;

class Goods extends BaseController
{

    use SoftDelete;
    protected $deleteTime='delete_time';
    protected $defaultSoftDelete = 'delete_time';
    //商品展示处理
    public function goodList()
    {
        $where = \app\admin\logic\Goods::getWhere();
        $list = \app\admin\model\Goods::goodList($where);
        View::assign('list', $list);

        $idd=\app\admin\logic\Goods::getIdd();

        return view();
    }
    public function goodCate()
    {


        return view();
    }
    //商品添加处理
    public function goodAdd()
    {
        //查询所有分类
        $cate = Category::getCateList()->toArray();
        //创建一个数组
        $clist = [];
        foreach ($cate as $v) {
            //查询一级分类
            if ($v['parentid'] == 0) {
                //把一级分类的主键id放到数组里当键值,再把数据添到空数组里拼成一维数组
                $clist[$v['id']] = $v;
                //再次循环查询二级分类
                foreach ($cate as $vv) {
                    //如果一级分类等于某些分类的副id的话,
                    if ($v['id'] == $vv['parentid']) {
                        //根据对应的一级分类添加一个next数组并把第二次循环的二级分类id放到数组里当主键
                        $clist[$v['id']]['next'][$vv['id']] = $vv;
                    }
                }
            }
        }
        View::assign('clist', $clist);
        if (Request::isPost()) {
            $post = Request::post();
            $data = [
                'name' => $post['name'],
                'pro_number' => $post['pro_number'],
                'price' => $post['price'],
                'price_shop' => $post['price_shop'],
                'photo_x'=>'UploadFiles/product//20210615/141b5f845c9df976dee0b7e50e6e498e.png',
                //'photo_string'=>$post['photo_string'],
                'content' => $post['content'],
                'sort' => $post['sort'],
                'renqi' => $post['renqi'],
                'addtime'=>time(),
                'num' => $post['num'],
                'cid' => $post['cid'],
                'company' => $post['author'],
                'is_show' => isset($post['is_show'])?'2':'1',
                'is_down' => isset($post['is_down'])?'2':'1',
                'is_hot' => isset($post['is_hot'])?'2':'1',
            ];
            $file=Upload::upload('img','images','md5');
            if($file){
                $data['photo_x']=$file;
            }
            try {
                $res = \app\admin\model\Goods::create($data);
                //成功的话调用config里的成功函数
                $msg = Config::get('msg.SUCCESS');
            } catch (\Exception $exception) {
                $res = false;
                //失败之后返回失败信息
                $msg = $exception->getMessage();
            }
            if ($res) {
                $this->success($msg, url('Goods/goodList'));
            } else {
                $this->error($msg);
            }
        }
        return view();

    }
    //商品修改处理
    public function goodExit()
    {
        //查询所有分类
        $cate = Category::getCateList()->toArray();
        //创建一个数组
        $clist = [];
        foreach ($cate as $v) {
            //查询一级分类
            if ($v['parentid'] == 0) {
                //把一级分类的主键id放到数组里当键值,再把数据添到空数组里拼成一维数组
                $clist[$v['id']] = $v;
                //再次循环查询二级分类
                foreach ($cate as $kk) {
                    //如果一级分类等于某些分类的副id的话,
                    if ($v['id'] == $kk['parentid']) {
                        //根据对应的一级分类添加一个next数组并把第二次循环的二级分类id放到数组里当主键
                        $clist[$v['id']]['next'][$kk['id']] = $kk;
                    }}}}
        View::assign('clist', $clist);
        $id=Request::get('id');
        //渲染添加页面
        if(isset($id)){

            $info=\app\admin\model\Goods::withTrashed()->find($id)->toArray();
            View::assign($info);
        }
        if (Request::isPost()) {
            $photo_x=Upload::upload('img','UploadFiles/product/','data');
            //判断是否是post传值
            $post = Request::post();
            $data = [
                'name' => $post['name'],
                'pro_number' => $post['pro_number'],
                'price' => $post['price'],
                'price_shop' => $post['price_shop'],
                'photo_x'=>$photo_x,
                //'photo_string'=>$post['photo_string'],
                'content' => $post['content'],
                'sort' => $post['sort'],
                'renqi' => $post['renqi'],
                'addtime'=>time(),
                'num' => $post['num'],
                'cid' => $post['cid'],
                'company' => $post['author'],
                'is_show' => isset($post['is_show'])?'2':'1',
                'is_down' => isset($post['is_down'])?'2':'1',
                'is_hot' => isset($post['is_hot'])?'2':'1',
            ];
            $data['id']=$post['id'];
            try {
                $res = \app\admin\model\Goods::update($data);
                //成功的话调用config里的成功函数
                $msg = Config::get('msg.SUCCESS');
            } catch (\Exception $exception) {
                $res = false;
                //失败之后返回失败信息
                $msg = $exception->getMessage();
            }
            if ($res) {
                $this->success($msg, url('Goods/goodList'));
            } else {
                $this->error($msg);
            }
        }
        return view();
    }
    //商品删除处理
    public function goodDel()
    {
        //获取对应点击事件要删除物品的主键id
        $delid=Request::get("delid");
        try{
            $goods=\app\admin\model\Goods::goodDel($delid);
            $msg='成功';
        }catch (\Exception $exception){
            $msg=$exception->getMessage();
            $goods=false;
        }
        if ($goods){
            $this->success($msg,"goodList");
        }else{
            $this->error($msg);
        }
    }
    //被删除商品的展示
    public function goodHui()
    {
        $list=\app\admin\model\Goods::onlyTrashed()->paginate(['list_rows'=>5]);
        View::assign('list',$list);
        return view();
    }
    //删除商品的恢复
    public function getGoodDel()
    {
        $ids=Request::get("huiid");
        if(isset($ids)){
            try{
                \app\admin\model\Goods::goodRollback($ids);
                $goods=true;
                $msg='成功';
            }catch (\Exception $exception){
                $goods=false;
                $msg= $exception->getMessage();

            }
            if ($goods){
                $this->success("$msg",url('goodList'),3);
            }else{
                $this->error($msg);
            }
        }
    }
}